local msg_queue = require 'eco.msg_queue'
local time = require 'eco.time'

local function sender(queue)
    for i = 1, 10 do
        local ok, err = queue:send("Message " .. i, 1)
        if i == 3 then
            queue:send_urgent("Urgent 1", 5)
        end
        if not ok then
            print("Sender: send timeout -", err)
       else
            print("Sender sent:", "Message " .. i)
            time.sleep(0.1)  -- 模拟发送间隔
       end
    end
end

local function receiver(queue)
    while true do
        local msg, err = queue:recv(2)
        if not msg then
            print("Receiver: receive timeout -", err)
        else   
            print("Receiver received:", msg)
            time.sleep(1.5)  -- 模拟处理时间
        end
    end
end

-- 初始化消息队列（最大 3 条消息）
local queue = msg_queue:new(5)

-- 启动发送和接收协程
local w1 = eco.run(sender, queue)
time.sleep(1.5) 
local w2 = eco.run(receiver, queue)
